package cn.lili.controller.goods;

import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.entity.dos.DraftGoods;
import cn.lili.modules.goods.entity.dto.DraftGoodsSearchParams;
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
import cn.lili.modules.goods.entity.vos.DraftGoodsVO;
import cn.lili.modules.goods.integration.GoodsIntegrationService;
import cn.lili.modules.goods.service.DraftGoodsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 店铺端,草稿商品接口
 *
 * @author paulG
 * @since 2021/2/20 2:26 下午
 */
@RestController
@Api(tags = "店铺端,草稿商品接口")
@RequestMapping("/goods/draft")
@RequiredArgsConstructor
public class DraftGoodsController {

    private final DraftGoodsService draftGoodsService;

    private final GoodsIntegrationService goodsIntegrationService;


    @ApiOperation(value = "分页获取草稿商品列表")
    @GetMapping
    public ResultMessage<Page<DraftGoods>> getDraftGoodsByPage(DraftGoodsSearchParams searchParams) {
        String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getExtendId();
        searchParams.setStoreId(storeId);
        return ResultUtil.data(draftGoodsService.getDraftGoods(searchParams));
    }

    @ApiOperation(value = "获取草稿商品")
    @GetMapping(value = "/{id}")
    public ResultMessage<DraftGoodsVO> getDraftGoods(@PathVariable String id) {
        return ResultUtil.data(draftGoodsService.getDraftGoods(id));
    }

    @ApiOperation(value = "保存草稿商品")
    @PostMapping
    public ResultMessage<String> saveDraftGoods(@RequestBody GoodsOperationDTO goodsOperationDTO) {
        goodsIntegrationService.saveDraftGoods(goodsOperationDTO);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除草稿商品")
    @DeleteMapping(value = "/{id}")
    public ResultMessage<String> deleteDraftGoods(@PathVariable String id) {
        draftGoodsService.deleteGoodsDraft(id);
        return ResultUtil.success();
    }

}
